<template>
	<div>
		<div>
			<div style="display: inline-block;margin-bottom: 6px;">
				<span>类型：</span>
				<el-select v-model="runStatusT" style="width:150px;margin-left: 10px;" @change="refresh()">
					<el-option v-for="item in statesnum" :key="item.value" :label="item.label" :value="item.value">
					</el-option>
				</el-select>&nbsp;&nbsp;&nbsp;&nbsp;
			</div>
			<el-button style="margin: 10px 0;" size="mini" type="primary" icon="document"
				@click="refresh">刷新</el-button>
			<el-button style="margin: 10px 0;" :disabled="!isAuth('runErrands:add')" size="mini" type="primary"
				icon="document" @click="advertNotice">添加</el-button>
		</div>
		<el-table v-loading="tableDataLoading" :data="guangaoData.records">
			<el-table-column fixed prop="id" label="编号" width="80"></el-table-column>
			<el-table-column prop="runStatus" label="类型">
				<template slot-scope="scope">
					<span v-if="scope.row.runStatus == 1">公里数</span>
					<span v-if="scope.row.runStatus == 2">重量</span>
					<span v-if="scope.row.runStatus == 3">时间</span>
				</template>
			</el-table-column>
			<el-table-column prop="startValue" label="起始值">
			</el-table-column>
			<el-table-column prop="endValue" label="结束值">
			</el-table-column>
			<el-table-column prop="runPrice" label="价格">
			</el-table-column>
			<el-table-column label="操作" width="180">
				<template slot-scope="scope">
					<el-button size="mini" type="primary" :disabled="!isAuth('runErrands:update')"
						@click="advertUpdates(scope.row)">修改</el-button>
					<el-button size="mini" type="danger" :disabled="!isAuth('runErrands:delete')"
						@click="advertdeletes(scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<div style="text-align: center;margin-top: 10px;">
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
				:page-sizes="[10, 20, 30, 40]" :page-size="limit" :current-page="page"
				layout="total,sizes, prev, pager, next,jumper" :total="guangaoData.total">
			</el-pagination>
		</div>
		<!-- 添加素材 -->
		<el-dialog title="添加" :visible.sync="dialogFormVisible" center>
			<div style="margin-bottom: 10px;">
				<span style="width: 200px;display: inline-block;text-align: right;">类型：</span>
				<el-radio-group v-model="runStatus" @change="runStatusBtn()">
					<el-radio :label="1">公里数</el-radio>
					<el-radio :label="2">重量</el-radio>
					<el-radio :label="3">时间</el-radio>
				</el-radio-group>
			</div>

			<div style="margin-bottom: 10px;">
				<span style="width: 200px;display: inline-block;text-align: right;">起始值：</span>
				<el-time-select v-if="runStatus==3" v-model="startValue" :picker-options="{
				    start: '00:00',
				    step: '00:1',
				    end: '23:59'
				  }" placeholder="选择起始时间" style="width:50%;">
				</el-time-select>
				<el-input v-else style="width:50%;" v-model="startValue" placeholder="请输入起始值" type="number">
				</el-input>
			</div>
			<div style="margin-bottom: 10px;">
				<span style="width: 200px;display: inline-block;text-align: right;">结束值：</span>
				<el-time-select v-if="runStatus==3" v-model="endValue" :picker-options="{
				    start: '00:00',
				    step: '00:1',
				    end: '23:59'
				  }" placeholder="选择结束时间" style="width:50%;">
				</el-time-select>
				<el-input v-else style="width:50%;" v-model="endValue" placeholder="请输入结束值" type="number">
				</el-input>
			</div>
			<div style="margin-bottom: 10px;">
				<span style="width: 200px;display: inline-block;text-align: right;">价格：</span>
				<el-input style="width:50%;" v-model="runPrice" placeholder="请输入价格" type="number">
				</el-input>
			</div>
			<div slot="footer" class="dialog-footer">
				<el-button @click="dialogFormVisible = false">取 消</el-button>
				<el-button type="primary" @click="advertNoticeTo()">确 定</el-button>
			</div>
		</el-dialog>
		<!-- 修改素材 -->
		<el-dialog title="修改" :visible.sync="dialogFormVisible1" center>
			<el-form :model="form">
				<el-form-item label="类型：" :label-width="formLabelWidth">
					<el-radio-group v-model="form.runStatus">
						<el-radio :label="1">公里数</el-radio>
						<el-radio :label="2">重量</el-radio>
						<el-radio :label="3">时间</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="起始值：" :label-width="formLabelWidth">
					<el-time-select v-if="form.runStatus==3" v-model="form.startValue" :picker-options="{
					    start: '00:00',
					    step: '00:1',
					    end: '23:59'
					  }" placeholder="选择起始时间" style="width:50%;">
					</el-time-select>
					<el-input v-else style="width:90%;" v-model="form.startValue" placeholder="请输入起始值"
						type="number"></el-input>
				</el-form-item>
				<el-form-item label="结束值：" :label-width="formLabelWidth">
					<el-time-select v-if="form.runStatus==3" v-model="form.endValue" :picker-options="{
					    start: '00:00',
					    step: '00:1',
					    end: '23:59'
					  }" placeholder="选择结束时间" style="width:50%;">
					</el-time-select>
					<el-input v-else style="width:90%;" v-model="form.endValue" placeholder="请输入结束值" type="number"></el-input>
				</el-form-item>
				<el-form-item label="价格：" :label-width="formLabelWidth">
					<el-input style="width:90%;" v-model="form.runPrice" placeholder="请输入价格" type="number"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="dialogFormVisible1 = false">取 消</el-button>
				<el-button type="primary" @click="amendadvertTo()">确 定</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		quillEditor
	} from 'vue-quill-editor'
	import 'quill/dist/quill.core.css'
	import 'quill/dist/quill.snow.css'
	import 'quill/dist/quill.bubble.css'
	import quillConfig from '../locality/quill-config.js'
	export default {
		components: {
			quillEditor
		},
		data() {
			return {
				page: 1,
				limit: 10,
				runStatus: '',
				startValue: '',
				endValue: '',
				runPrice: '',
				id: 3,
				runStatusT: '',
				form: {
					id: '',
					runStatus: '',
					startValue: '',
					endValue: '',
					runPrice: '',
				},
				formLabelWidth: '200px',
				tableDataLoading: false,
				dialogFormVisible1: false,
				dialogFormVisible: false,
				guangaoData: [],
				quillOption: quillConfig,
				statesnum: [{
						value: '',
						label: '全部'
					},
					{
						value: 1,
						label: '公里数'
					},
					{
						value: 2,
						label: '重量'
					},
					{
						value: 3,
						label: '时间'
					},

				],
			}
		},
		methods: {
			handleAvatarSuccess(file) {
				this.imgUrl = file.data
				this.linkUrl = this.imgUrl
			},
			handleAvatarSuccess1(file) {
				this.form.imgUrl = file.data
				this.form.linkUrl = this.form.imgUrl
			},
			handleSizeChange(val) {
				this.limit = val
				this.guangaoSelect()
			},
			handleCurrentChange(val) {
				this.page = val
				this.guangaoSelect()
			},
			// 刷新
			refresh() {
				this.guangaoSelect()
			},
			// 添加素材弹框
			advertNotice() {
				this.id = ''
				this.runStatus = ''
				this.startValue = ''
				this.endValue = ''
				this.runPrice = ''
				this.dialogFormVisible = true
			},
			// 添加
			advertNoticeTo() {
				if (this.runStatus == '') {
					this.$notify({
						title: '提示',
						duration: 1800,
						message: '请选择类型',
						type: 'warning'
					})
					return
				}
				if (this.startValue === '') {
					this.$notify({
						title: '提示',
						duration: 1800,
						message: '请输入起始值',
						type: 'warning'
					})
					return
				}
				if (this.endValue === '') {
					this.$notify({
						title: '提示',
						duration: 1800,
						message: '请输入结束值',
						type: 'warning'
					})
					return
				}
				if (this.runPrice === '') {
					this.$notify({
						title: '提示',
						duration: 1800,
						message: '请输入价格',
						type: 'warning'
					})
					return
				}
				this.$http({
					url: this.$http.adornUrl('admin/errands/addErrandConfiguration'),
					method: 'post',
					// data: this.$http.adornData({
					params: this.$http.adornParams({
						'runStatus': this.runStatus,
						'startValue': this.startValue,
						'endValue': this.endValue,
						'runPrice': this.runPrice,
					})
				}).then(({
					data
				}) => {
					if (data.code == 0) {
						this.dialogFormVisible = false
						this.$message({
							message: '操作成功',
							type: 'success',
							duration: 1500,
							onClose: () => {

								this.guangaoSelect()
							}
						})
					} else {
						this.$message({
							message: data.msg,
							type: 'warning',
							duration: 1500,
							onClose: () => {}
						})
					}

				})
			},
			runStatusBtn(){
				this.endValue = ''
				this.startValue = ''
				this.form.endValue = ''
				this.form.startValue = ''
			},
			// 点击修改
			advertUpdates(rows) {
				this.form.id = rows.id
				this.form.runStatus = rows.runStatus
				this.form.startValue = rows.startValue
				this.form.endValue = rows.endValue
				this.form.runPrice = rows.runPrice

				this.dialogFormVisible1 = true
			},
			// 确定修改
			amendadvertTo() {
				if (this.form.runStatus == '') {
					this.$notify({
						title: '提示',
						duration: 1800,
						message: '请选择类型',
						type: 'warning'
					})
					return
				}
				if (this.form.startValue === '') {
					this.$notify({
						title: '提示',
						duration: 1800,
						message: '请输入起始值',
						type: 'warning'
					})
					return
				}
				if (this.form.endValue === '') {
					this.$notify({
						title: '提示',
						duration: 1800,
						message: '请输入结束值',
						type: 'warning'
					})
					return
				}
				if (this.form.runPrice === '') {
					this.$notify({
						title: '提示',
						duration: 1800,
						message: '请输入价格',
						type: 'warning'
					})
					return
				}
				this.$http({
					url: this.$http.adornUrl('admin/errands/addErrandConfiguration'),
					method: 'post',
					// data: this.$http.adornData({
					params: this.$http.adornParams({
						'id': this.form.id,
						'runStatus': this.form.runStatus,
						'startValue': this.form.startValue,
						'endValue': this.form.endValue,
						'runPrice': this.form.runPrice,
					})
				}).then(({
					data
				}) => {
					if (data.code == 0) {
						this.$message({
							message: '操作成功',
							type: 'success',
							duration: 1500,
							onClose: () => {
								this.guangaoSelect()
								this.dialogFormVisible1 = false
							}
						})
					} else {
						this.$notify({
							title: '提示',
							duration: 1800,
							message: data.msg,
							type: 'warning'
						})
					}

				})
			},
			// 删除广告
			advertdeletes(row) {
				this.$confirm(`确定删除此条信息?`, '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					this.$http({
						url: this.$http.adornUrl('admin/errands/deleteErrsConfiguration'),
						method: 'post',
						params: this.$http.adornParams({
							'id': row.id
						})
					}).then(({
						data
					}) => {
						if (data.code == 0) {
							this.$message({
								message: '删除成功',
								type: 'success',
								duration: 1500,
								onClose: () => {
									this.guangaoSelect()
								}
							})
						} else {
							this.$notify({
								title: '提示',
								duration: 1800,
								message: data.msg,
								type: 'warning'
							})
						}

					})
				})
			},
			// 获取素材数据
			guangaoSelect() {
				let page = this.page
				this.tableDataLoading = true
				this.$http({
					url: this.$http.adornUrl('admin/errands/selectErrConfigurationList'),
					method: 'get',
					params: this.$http.adornParams({
						'page': this.page,
						'limit': this.limit,
						'runStatus': this.runStatusT,
					})
				}).then(({
					data
				}) => {
					this.tableDataLoading = false
					let returnData = data.data
					this.guangaoData = returnData
				})
			},
			
		},
		mounted() {
			this.guangaoSelect()
		}
	}
</script>

<style>
	.customWidth {
		width: 80% !important;
	}
</style>